package com.weeho.dongyong.service;

import com.weeho.dongyong.dao.TaskDao;
import com.weeho.dongyong.entity.Approval;
import com.weeho.dongyong.entity.Task;
import com.weeho.dongyong.entity.TaskDetail;
import com.weeho.dongyong.utils.DateUtils;
import com.weeho.dongyong.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TaskService {
    @Autowired
    private TaskDao taskDao;

    @Autowired
    private TaskDetailService taskDetailService;
    @Autowired
    private ApprovalService approvalService;

    private String format = "yyyy-MM-dd hh:mm:ss";

    public int insertSelective(Task record) {
        record.setCreateTime(DateUtils.parseToString(new Date()));
        return taskDao.insertSelective(record);
    }

    public int updateByPrimaryKeySelective(Task record) {
        return taskDao.updateByPrimaryKeySelective(record);
    }

    public int deleteByPrimaryKey(String id) {
        return taskDao.deleteByPrimaryKey(id);
    }

    public Task selectByPrimaryKey(String id) {
        return taskDao.selectByPrimaryKey(id);
    }

    public List<Task> selectByCondition(Task record) {
        return taskDao.selectByCondition(record);
    }

    /**
     *
     * @param task
     * @param taskDetailList
     * @return task's id
     */
    public String  addTask(Task task, List<TaskDetail> taskDetailList) {

        int flag = 0;
        task.setId(StringHelper.generatePrimaryKey());
        flag += insertSelective(task);

        for(TaskDetail taskDetail : taskDetailList){
            taskDetail.setId(StringHelper.generatePrimaryKey());
            taskDetail.setFkTaskId(task.getId());
            flag += taskDetailService.insertSelective(taskDetail);
        }
        flag += approvalService.add(task);
        return task.getId();
    }


    public List sort(List<Approval> approvals) throws Exception{
        if(approvals == null || approvals.size()==0) return null;
        Map<String,List<Task>> map =  new TreeMap<String, List<Task>>(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }

        });

        for(Approval a : approvals){
            Task task = selectByPrimaryKey(a.getFkTaskId());

            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            String date = sd.format(DateUtils.parseToDateByFormat(task.getCreateTime(),format));

            if(map.get(date) == null){
                map.put(date,new ArrayList<Task>());
            }
            task.setState(approvalService.getTaskApprovalState(task.getId()));
            map.get(date).add(task);

        }
//        return map;

        List list = new ArrayList<>();
        for(Map.Entry<String,List<Task>> entry: map.entrySet()){
            Map<String,Object> tmp = new HashMap();
            tmp.put("date",entry.getKey());
            tmp.put("data",entry.getValue());
            list.add(tmp);
        }

        return list;



    }
}